package com.xiaohe.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiaohe.common.Result;
import com.xiaohe.dao.WayDao;
import com.xiaohe.entity.Way;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;

@RestController
@RequestMapping("/api/user")
@CrossOrigin
public class WayController {

        @Resource
        WayDao userMapper;

        @PostMapping
        public Result<?> save(@RequestBody Way user) {
            userMapper.insert(user);
            return Result.success(1);
        }

        @PutMapping
        public Result<?> updata(@RequestBody Way user) {
            userMapper.updateById(user);
            return Result.success(1);
        }

        @DeleteMapping("/{id}")
        public Result<?> delete(@PathVariable Long id) {
            userMapper.deleteById(id);
            return Result.success(1);
        }

        @GetMapping
        public Result<?> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                                  @RequestParam(defaultValue = "10") Integer pageSize,
                                  @RequestParam(defaultValue = "") String search) {
            LambdaQueryWrapper<Way> wrapper = Wrappers.<Way>lambdaQuery();
            if (StrUtil.isNotBlank(search)) {
                wrapper.like(Way::getNickName, search);
            }
            Page<Way> userPage = userMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);
            return Result.success(userPage);
        }

}
